package org.javaboy.security_jdbc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.JdbcUserDetailsManager;

import javax.sql.DataSource;
import javax.xml.crypto.Data;

@Configuration
public class SecurityConfig {

    @Bean
    UserDetailsService us(JdbcTemplate jdbcTemplate) {
        JdbcUserDetailsManager manager = new JdbcUserDetailsManager();
        manager.setJdbcTemplate(jdbcTemplate);
        if (!manager.userExists("zhangsan")) {
            manager.createUser(User.withUsername("zhangsan").password("{noop}123").authorities("admin").build());
        }
        if (!manager.userExists("ls")) {
            manager.createUser(User.withUsername("ls").password("{noop}123").authorities("user").build());
        }
        return manager;
    }

}
